/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.attach;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;

/**
 * @author Alucard (email:357375084@qq.com)
 * @since 2013-03-11
 */
@Service("attachlinkService")
public class AttachLinkServiceImpl implements AttachLinkService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of AttachLink
     ****************************************/
    public void insert(AttachLink attachlink) throws Exception {
        iDataSet4iBatis.insert(attachlink);
    }

    /****************************************
     * Update a instace of AttachLink
     ****************************************/
    public void update(AttachLink attachlink) throws Exception {
        iDataSet4iBatis.update(attachlink);
    }

    /****************************************
     * Delete a instace of AttachLink
     ****************************************/
    public void delete(AttachLink attachlink) throws Exception {
        iDataSet4iBatis.delete(attachlink);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(AttachLink attachlink) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(AttachLink attachlink) throws Exception {
        String statementId = attachlink.getStatmentId();
        String mainId = attachlink.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> attachlinkList = new ArrayList<BaseModel>();
        for (String id : ids) {
            AttachLink model = new AttachLink();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            attachlinkList.add(model);
        }
        iDataSet4iBatis.batchDelete(attachlinkList);
    }

    /****************************************
     * Query instaces of AttachLink
     ****************************************/
    public List<BaseModel> queryAsList(AttachLink attachlink) throws Exception {
        String queryListStatement = "";
        if (attachlink.isPaging()) {
            queryListStatement = attachlink.getStatmentId();
            attachlink.setStatmentId("AttachLink.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(attachlink);
            attachlink.setTotalCount(baseModel.getTotalCount());
            attachlink.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(attachlink);
    }

    /****************************************
     * Query a instaces of AttachLink
     ****************************************/
    public BaseModel queryAsObject(AttachLink attachlink) throws Exception {
        return iDataSet4iBatis.queryAsObject(attachlink);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
